package cn.stronglink.assetmanage.business.repair.detail;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import Scaner.scan.IScanerListener;
import Scaner.scan.TimerInfraraedScaner;
import cn.stronglink.assetmanage.BaseActivity;
import cn.stronglink.assetmanage.R;
import cn.stronglink.assetmanage.dao.DaoManager;
import cn.stronglink.assetmanage.dao.RepailDetailDao;
import cn.stronglink.assetmanage.dao.RepairDao;
import cn.stronglink.assetmanage.dao.SearchAssetDao;
import cn.stronglink.assetmanage.entity.RepailDetail;
import cn.stronglink.assetmanage.entity.Repair;
import cn.stronglink.assetmanage.entity.SearchAsset;
import cn.stronglink.assetmanage.entity.User;
import cn.stronglink.assetmanage.utils.Constants;
import cn.stronglink.assetmanage.utils.LogUtil;
import cn.stronglink.assetmanage.utils.ToastUtil;
import cn.stronglink.assetmanage.utils.dao.RepairDataUtil;

public class RepairDetailActivity extends BaseActivity implements OnClickListener, IScanerListener {
    //保存按钮
    private Button btn_repairDetail_save;
    private TextView tv_repair_detail_no, et_repair_detail_assis_name, tv_repair_detail_assis_type,
            et_repair_detail_note;
    private TextView tv_repair_detail_date, tv_repair_detail_people, et_repair_detail_rfid,
            et_repair_detail_assis_sn;
    private String snOrRfid, orderId;
    private Repair mRepair;
    private RepailDetail mRepailDetail;
    private TimerInfraraedScaner mScaner;
    private boolean isSn = false;
    private static final String TAG = "RepairDetailActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repair_detail);
        initDataAndView();
    }

    private void initDataAndView() {
        initView();
        initData();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        initDataAndView();
    }

    private void initView() {
        btn_repairDetail_save = findViewById(R.id.btn_repairDetail_save);
        tv_repair_detail_people = findViewById(R.id.tv_repair_detail_people);
        tv_repair_detail_people.setText(User.getInstance().getName());
        tv_repair_detail_date = findViewById(R.id.tv_repair_detail_date);
        et_repair_detail_rfid = findViewById(R.id.et_repair_detail_rfid);
        et_repair_detail_rfid.setOnClickListener(this);
        et_repair_detail_assis_sn = findViewById(R.id.et_repair_detail_assis_sn);
        et_repair_detail_assis_sn.setOnClickListener(this);
        btn_repairDetail_save.setOnClickListener(this);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        tv_repair_detail_date.setText(format.format(new Date()));
        tv_repair_detail_no = findViewById(R.id.tv_repair_detail_no);
        et_repair_detail_assis_name = findViewById(R.id.et_repair_detail_assis_name);
        tv_repair_detail_assis_type = findViewById(R.id.tv_repair_detail_assis_type);
        et_repair_detail_assis_sn = findViewById(R.id.et_repair_detail_assis_sn);
        et_repair_detail_note = findViewById(R.id.et_repair_detail_note);
    }

    private void initData() {
        snOrRfid = getIntent().getStringExtra(Constants.REPAIR_DETAIL_NO);
        orderId = getIntent().getStringExtra(Constants.REPAIR_REGISTER_ORDER);
        if (orderId == null || "".equals(orderId)) {
            ToastUtil.toastShort("表单id为空，不能操作数据");
            return;
        }
        mRepailDetail = DaoManager.getDaoSession(this).getRepailDetailDao().queryBuilder().
                whereOr(RepailDetailDao.Properties.SerialNo.eq(snOrRfid), RepailDetailDao.Properties.RfidNo.eq(snOrRfid))
                .where(RepailDetailDao.Properties.RepairId_.eq(orderId)).unique();
        mRepair = DaoManager.getDaoSession(this).getRepairDao().queryBuilder().
                where(RepairDao.Properties.Id_.eq(orderId)).unique();
        showDetailData(mRepailDetail);
        mScaner = new TimerInfraraedScaner(this);
        mScaner.setScanerListener(this);
    }


    private void showDetailData(RepailDetail repailDetail) {
        tv_repair_detail_no.setText(orderId);
        if (repailDetail != null) {
            et_repair_detail_assis_name.setText(repailDetail.getAssetName());
            et_repair_detail_assis_sn.setText(repailDetail.getSerialNo());
            et_repair_detail_rfid.setText(repailDetail.getRfidNo());
            tv_repair_detail_assis_type.setText(repailDetail.getTypeName());
            et_repair_detail_note.setText(repailDetail.getRemark());
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.et_repair_detail_rfid:
                //扫描二维码
//                scanQr();
                break;
            case R.id.et_repair_detail_assis_sn:
//                scanSn();thit
                break;
            case R.id.btn_repairDetail_save:
                saveRepairData();
                break;
            default:
                break;
        }
    }

    private void saveRepairData() {
        if (mRepailDetail != null) {
            List<RepailDetail> repailDetailList = DaoManager.getDaoSession(this).
                    getRepailDetailDao().queryBuilder().where(RepailDetailDao.Properties.RepairId_.eq(orderId))
                    .list();
            mRepair.setDetailList(repailDetailList);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
            mRepailDetail.setStatus(Constants.STATUS_THREE);
            mRepailDetail.setExecuteTime(format.format(new Date()));
            mRepailDetail.setRemark(et_repair_detail_note.getText().toString());
            repailDetailList.add(mRepailDetail);
            RepairDataUtil.saveRepairDetailata(mRepair);
            onBackPressed();
        } else {
            ToastUtil.toastShort("维修详情信息为空");
        }
    }


    private void scanQr() {
        isSn = false;
        if (mScaner == null) {
            mScaner = new TimerInfraraedScaner(this);
            mScaner.setScanerListener(this);
        }
        mScaner.playSingle();
    }

    private void scanSn() {
        isSn = true;
        if (mScaner == null) {
            mScaner = new TimerInfraraedScaner(this);
            mScaner.setScanerListener(this);
        }
        mScaner.playSingle();
    }

    @Override
    protected void onResume() {
        super.onResume();
        setSelectPosition(4);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            if (mScaner != null) {
                this.mScaner.stop();
                this.mScaner.destory();
            }
        } catch (Exception e) {
            LogUtil.e(TAG, e.getMessage());
        }
    }

    @Override
    public void onScaning(List<String> data) {
        String resultData = data.get(data.size() - 1);
        if (isSn) {
            et_repair_detail_assis_sn.setText(resultData);
        } else {
            et_repair_detail_rfid.setText(resultData);
        }
    }
}
